<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class AdminModel
{
    public static function addId($param){
        return DB::table('t_admin')->insertGetId($param);
    }

    public static function add($param){
        return DB::table('t_admin')->insert($param);
    }

    public static function queryFirst($where,$field){
        return DB::table('t_admin')->where($where)->first($field);
    }

    public static function queryFirstOrWhere($where,$orWhere,$field){
        return DB::table('t_admin')->where($where)->orWhere($orWhere)->first($field);
    }

    public static function queryLogin($where){
        $field = ['t_admin.id','t_admin.username','t_admin.nickname','t_admin.avatar','t_admin.school'
            ,'v.name','g.title','g.roule','t_admin.login_status','t_admin.sn_num'];
        return DB::table('t_admin')
            ->leftJoin('t_vip as v','t_admin.vip_id','=','v.id')
            ->leftJoin('t_group as g','t_admin.t_group_id','=','g.id')
            ->where($where)
            ->first($field);
    }

    public static function edit($where,$param){
        return DB::table('t_admin')->where($where)->update($param);
    }

    public static function queryGetName($where){
        return DB::table('t_admin')->where($where)->value('username');
    }

    public static function queryGet($where,$field){
        return DB::table('t_admin')->where($where)->get($field);
    }

    public static function queryCount($where=[],$param=[]){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('id');
        }
        return DB::table('t_admin')
            ->where($where)
            ->count('id');
    }

    public static function queryAll($where=[],$param=[],$page){
        $field = ['a.id','a.username','nickname','a.create_time','login_time','login_ip','email',
            'status','apk_num','sn_num','login_status','mobile','v.name','g.title'];
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin as a')
                ->leftJoin('t_vip as v','a.vip_id','=','v.id')
                ->leftJoin('t_group as g','a.t_group_id','=','g.id')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field);
        }
        return DB::table('t_admin as a')
            ->leftJoin('t_vip as v','a.vip_id','=','v.id')
            ->leftJoin('t_group as g','a.t_group_id','=','g.id')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field);
    }

    public static function queryLeftVip($where){
        $field = ['a.apk_num','v.vip_size','v.vip_num','v.upgrade'];
        return DB::table('t_admin as a')
            ->leftJoin('t_vip as v','a.vip_id','=','v.id')
            ->where($where)
            ->first($field);
    }

    public static function queryWhereSum($where,$field){
        return DB::table('t_admin')->where($where)->sum("$field");
    }

    public static function login($where,$orWhere,$field){
        return DB::table('t_admin')->where($where)->orWhere($orWhere)->first($field);
    }

    public static function insertGetId($param){
        return DB::table('t_admin')->insertGetId($param);
    }

    
}